#!/bin/bash

# shellcheck disable=SC2034
#   desc:平台初始化脚本主要用于部署初始化时使用
#   可重复执行，但会覆盖之前的改动.
#   功能：1.初始化安装 
#        2.初始化数据库
#        3.启动应用
#   updater: dingxingwei
#   date: 2020/4/16
#
#######################################

set -e
set -o pipefail
# -----------------------变量定义---------------------

RED_COLOR="\033[1m\033[31m"
GREEN_COLOR="\033[1m\033[32m"
YELLOW_COLOR="\033[1m\033[33m"
TAIL_COLOR="\033[0m"

tomcat_version='apache-tomcat-8.5.75'
tomcat_url="https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.75/bin/$tomcat_version.tar.gz"
jenkins_url='https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.319.2/jenkins.war'


# -----------------------函数定义---------------------
function init(){
    yum install net-tools -y
    yum install python3-devel -y
    yum install java -y
}

# 安装数据库并初始化数据
function syuee_mariadb(){
    yum install mariadb-server mariadb -y
    systemctl restart mariadb
    systemctl enable mariadb
    mysql < docs/sql-scripts/privileges.sql
    mysql < docs/sql-scripts/schema.sql
}

#安装redis并初始化数据
function syuee_redis(){
    yum install redis -y
    /bin/cp -rf docs/redis.conf /etc/redis.conf
    systemctl restart redis
    systemctl enable redis
}
#安装nginx并初始化数据
function syuee_nginx(){
    yum install nginx  -y
    /bin/cp -rf docs/nginx.conf /etc/nginx/nginx.conf
    systemctl restart nginx 
    systemctl enable nginx
}

#部署应用
function install_app(){
    pip3 install uwsgi
    cd /home/syuee/server/ && pip3 install -r requirements.txt && cd ..
    cd /home/syuee/server/ && uwsgi  --ini syuee.ini && cd ..
}

function jenkins_tomcat(){
    wget $tomcat_url
    wget $jenkins_url
    tar xf ${tomcat_version}.tar.gz -C /usr/local/
    ln -s /usr/local/${tomcat_version} /usr/local/tomcat
    rm -rf /usr/local/tomcat/webapps/*
    cp jenkins.war /usr/local/tomcat/webapps/
    /usr/local/tomcat/bin/startup.sh
}

function main(){
    init
    syuee_mariadb
    syuee_redis
    syuee_nginx
    install_app
    jenkins_tomcat
}

main













